<?php
/**
 * Created by PhpStorm.
 */

namespace App\Http\Middleware;

use App\Http\Service\Admin\ActionLogService;
use Closure;
use Illuminate\Http\Request;

class AdminActionLog
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if ($request->method() != 'GET') {
            $admin = admin_info();
            $post_data = [
                'method' => $request->method(),
                'route' => $request->route()->getName(),
                'table_id' => $request->input()['id'] ?? '',
                'actinname' => $request->route()->getActionName(),
                'ip' => $request->ip(),
                'admin_id' => $admin['id'],
                'admin_name' => $admin['name']
            ];
            ActionLogService::insert($post_data);
        }

        return $next($request);
    }
}